#!/bin/bash
message() {
    echo "$@" 1>&2
}
PROG=${0##*/}
usage() {
    cat <<EO_ECHO
usage: $PROG -r REPO [ -i | -s ] [ -u USER -p PASSWD ]
  -i, --initialize        run initialization of a clean repository
  -s, --synchronize       run synchronization on a ready repository
  -r, --repository=REPO   specify a repository with an ID, you must
                          insert it manually in this script if it is
                          not present
  -u, --username=USER     specify username for authentication
  -p, --password=PASSWD   specify password for authentication

                     NOTE:
                          The username and password can also be
                          filled in the script, currently the
                          command line arguments have no effect.

Author: zerox (zxia@helixmicro.com.cn)
Send bug report/comments to zeroxia@gmail.com
EO_ECHO
    exit 0
}

## options parsing
parsed=$(getopt --shell bash --name ${0##*/} \
    --options r:u:p:ish \
    --longoptions repository:,username:,password:,initialize,synchronize,help \
    -- "$@")
if [[ $? -ne 0 ]]; then
    message "error when parsing options, quit..."
    exit 1
fi
eval set -- "$parsed"
while true; do
    case "$1" in
        -h|--help        ) usage ;;                      # exit immediately
        -i|--initialize  ) do_what=init;     shift 1 ;;
        -s|--synchronize ) do_what=sync;     shift 1 ;;
        -r|--repository  ) repo=$2;          shift 2 ;;
        -u|--username    ) cmd_username=$2;  shift 2 ;;
        -p|--password    ) cmd_password=$2;  shift 2 ;;
        --               ) shift; break ;;               # end of command line options
        *) # invalid option
        message "error: unknown option: \`$arg'"
        message "Try run the script with argument \`-h' or \`--help'"
        exit 2
        ;;
    esac
done

if [[ x$repo == x ]]; then
    message "error: you must specify the repository."
    exit -1
fi

if [[ x$do_what == x ]]; then
    do_what=sync
fi

case $repo in
    csie )
        source=http://opensvn.csie.org/zerox
        dest=file:///d:/zerox/work/script/sync/csie
        username=${cmd_username:-zeroxia}
        password=${cmd_password:-zeroxia}
        ;;
    google )
        source=http://zerox.googlecode.com/svn
        dest=file:///d:/zerox/work/script/sync/google
        username=${cmd_username:-zeroxia}
        password=${cmd_password:-x7q5t9e7}
        ;;
    local )
        source=http://opensvn.csie.org/zerox
        dest=http://localhost/svn/script
        username=${cmd_username:-zeroxia}
        password=${cmd_password:-zeroxia}
        ;;
    * )
        message "error: $repo: unknown repository, quit..."
        exit 1
        ;;
esac
auth_str="--username $username --password $password"
message "? auth. string: $auth_str"

begin_date=$(date)
begin_time=$(date +%s)
message "? operation begin: ${begin_date}"
if [[ x$do_what == xinit ]]; then
    message "-run initialization:"
    message "-target: $dest"
    message "-source: $source"
    svnsync initialize $dest $source $auth_str
elif [[ x$do_what == xsync ]]; then
    message "-run synchronization:"
    message "-target: $dest"
    message "-source: $source"
    svnsync synchronize $dest $auth_str
else
    message "error: unknown operation: $do_what. quit..."
    exit -1
fi
end_date=$(date)
end_time=$(date +%s)
message "? operation begin: ${begin_date}"
message "? operation   end: ${end_date}"
message "? time   consumed: $((end_time - begin_time)) second(s)"

# SVN information
# $LastChangedRevision: 141 $
# $LastChangedBy: zeroxia $
# $LastChangedDate: 2007-07-19 11:04:25 +0800 (Thu, 19 Jul 2007) $
# $HeadURL: https://opensvn.csie.org/zerox/cpreport/trunk/repo-sync.sh $
